# 使用mixin，给map get and set
class MappingMixin:
    def __getitem__(self, key):
        return self.__dict__.get(key)

    def __setitem__(self, key, value):
        return self.__dict__.setdefault(key, value)


class ReprMixin:
    def __repr__(self):
        s = self.__class__.__name__ + '('
        for k, v in self.__dict__.items():
            if not k.startswith('_'):
                s += '{}={}, '.format(k, v)
        s = s.rstrip(', ') + ')'  # 将最后一个逗号和空格换成括号
        return s

# 使用多继承，这样person拥有了多个mixin的能力，功能混合到person，不建议这么干
# 单一职责原则更合适
class Person(MappingMixin, ReprMixin):
    def __init__(self, name, gender, age):
        self.name = name
        self.gender = gender
        self.age = age


# 通过实例来访问属性
person = Person("小", "男", 22)
# 拥有了map的能力
print(person["name"])
print(person.name)
